<?php 

Class formsModel extends appModel
{
	/**
	 * 获取表单列表
	 * @param $page
	 * @param $limit
	 * @return 
	 */
	public function forms_list($page, $limit = 10)
	{
		$sql = "SELECT id,name,is_enable,created FROM ".$this->table('forms');
		$result = $this->show_page($sql, $page, $limit);
		if($result)
		{
			foreach($result['result'] as  $k => $v)
			{
				$result['result'][$k]['name'] = hy_dig_filter($v['name'],1);
			} 
		}
		$listPage['pagelink'] = $result['page'];
	    $listPage['data'] = $result['result'];
	    return $listPage;
	}
	
	/**
	 * 获取表单数据
	 * @param unknown $id
	 * @param unknown $page
	 * @param unknown $limit
	 * @return Ambigous <NULL, string>
	 */
	public function log_list($id, $val, $page, $limit = 10)
	{
		$data = implode(",", $val);
		$sql = "SELECT ".$data." FROM ".$this->table('forms_log_'.$id)." order by id desc";
		$result = $this->show_page($sql, $page, $limit);
		$listPage['pagelink'] = $result['page'];
		$listPage['data'] = $result['result'];
		return $listPage;
	}
	
	/**
	 * 获得表单数据的表头
	 * @param $id
	 * @param $type
	 * @return 
	 */
	public function get_table_list($id, $type = 0)
	{
		$arr['table'][] = "ID";
		$arr['val'][] = 'id';
		$where = "where a.form_id=".$id;
		if($type == 0)
		{
			$where .= " and a.is_list=1";
		}
		$sql = "select b.name, b.data from ".$this->table('forms_word')." as a left join ".$this->table('fields')." as b on a.fields_id=b.id ".$where;
		$result = $this->getAll($sql);
		if($result)
		{
			foreach($result as $k => $v)
			{
				$arr['table'][] = hy_dig_filter($v['name'], 1);
				$arr['val'][] = $v['data'];
			}
		}
		return $arr;
	}
	
	/**
	 * 获取表单所有信息
	 * @param $id
	 * @param $val
	 * @return
	 */
	public function get_all_info_log($id, $val)
	{
		$sql = "select * from ".$this->table('forms_log')."_".$id;
		$res = $this->getAll($sql);
		$arr = array();
		if($res)
		{
			foreach($res as $k => $v)
			{
				foreach($val as $vv)
				{
					$arr[$k][] = $v[$vv];
				}
			}
		}
		return $arr;
	}
	
	/**
	 * 获得表单名称
	 */
	public function get_form_name($id)
	{
		$sql = "select name from ".$this->table('forms')." where id=".$id;
		return $this->getOne($sql);
	}
	
	/**
	 * 删除表单数据
	 * @param $form_id
	 * @param $id
	 */
	public function del_log($form_id, $id)
	{
		if($form_id && $id)
		{
			$sql = "delete from ".$this->table('forms_log')."_".$form_id." where id=".$id;
			$this->query($sql);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 获取表单配置详情
	 * @param $id
	 * @return 
	 */
	public function get_form($id)
	{
		$sql="SELECT name, id, submit_interval, stime, etime FROM ".$this->table('forms')." WHERE id='".$id."'";
		$result=$this->getRow($sql);
		if($result)
		{
			if($result['stime'] != 0)
			{
				$result['stime'] = date('Y-m-d', $result['stime']);
			}
			if($result['etime'] != 0)
			{
				$result['etime'] = date('Y-m-d', $result['etime']);
			}
		}
		return $result;
	}
	
	/**
	 * 获得所有表单
	 * @return 
	 */
	public function get_all_forms()
	{
		$sql="SELECT name,id FROM ".$this->table('forms')."";
		$result=$this->getAll($sql);
		if($result)
		{
			foreach($result as  $k=>$v)
			{
				$result[$k]['name']=hy_dig_filter($v['name'],1);
			}
		}
		return $result;
	}
	
	/**
	 * 更新表单配置信息
	 * @param $info
	 * @param $id
	 */
	public function update_forms($info, $id)
	{
		if($id)
		{
			$data = yf_set_update($info);
			$sql = "update ".$this->table('forms')." set ".$data." where id=".$id;
			$this->query($sql);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 创建表单
	 * @param $info
	 * @return
	 */
	public function insert_forms($info)
	{
		if($info)
		{
			$data = yf_set_insert($info);
			$sql = "insert into ".$this->table('forms')." (".$data['key'].") values (".$data['val'].")";
			$this->query($sql);
			$id = $this->insert_id();
			if($id)
			{
				$sql = "CREATE TABLE ".$this->table('forms_log')."_".$id."(
				`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
				PRIMARY KEY(id)
				)ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
				$this->query($sql);
				return true;
			}
			else 
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 获取字段详细信息
	 * @param $id
	 * @return 
	 */
	public function get_field($id)
	{
		if($id)
		{
			$sql = "SELECT a.*  FROM ".$this->table('fields')." as a WHERE a.id = '".$id."'";
			$result = $this->getRow($sql);
			if($result)
			{
				$arr = hy_dig_filter($result['name'],3);
				$arr2 = hy_dig_filter($result['default_val'],3);
				$arr3 = hy_dig_filter($result['tips'],3);
				$arr4 = hy_dig_filter($result['field'],3);
				$result['en_tips'] = stripslashes($arr3[1]);
				$result['tips'] = stripslashes($arr3[0]);
				$result['en_field'] = explode('::',$arr4[1]);
				$result['field'] = explode('::',$arr4[0]);
				$result['en_name'] = $arr[1];
				$result['name'] = $arr[0];
				$result['en_default'] = $arr2[1];
				$result['default'] = $arr2[0];
				
				if(!empty($result['field']))
				{	
					if(is_array($result['field']))
					{   
						$temp = array();
						foreach($result['field'] as $k => $v)
						{
							$temp['field'] = $v;
							$val = json_decode($v);
							$temp['key'] = $val->k;
							$temp['val'] = $val->v;
							$temp['thumb'] = $val->t;
							$result['field'][$k] = $temp;
						}
					}
				}
				else 
				{
					$result['field'] = '';
				}
				
				if(!empty($result['en_field']))
				{
					if(is_array($result['en_field']))
					{   
						$temp = array();
						foreach($result['en_field'] as $kk => $vv)
						{
							$temp['field'] = $vv;
							$val = json_decode($vv);
							$temp['key'] = $val->k;
							$temp['val'] = $val->v;
							$temp['thumb'] = $val->t;
							$result['en_field'][$kk] = $temp;
						}
					}
				}
				else 
				{
					$result['en_field'] = '';
				}
				return $result;
			}
			else
			{
				return $result = array();
			}
		}
		return array();
	}
	
	/**
	 * 获取所有字段信息
	 * @return 
	 */
	public function get_all_field()
	{
		$sql = "SELECT a.id, a.name, a.type, a.data FROM (".$this->table('fields')." as a)  ORDER BY a.id DESC";
		$res = $this->getAll($sql);
		foreach($res as $k => $v)
		{
		  	$res[$k]['name'] = hy_dig_filter($v['name'], 1);
			switch($v['type'])
			{
				case 0:
					$res[$k]['type'] = '单行文本';
					break;
				case 1:
					$res[$k]['type'] = '多行文本';
					break;
				case 2:
					$res[$k]['type'] = '单选按钮';
					break;
				case 3:
					$res[$k]['type'] = '多选按钮';
					break;
				case 4:
					$res[$k]['type'] = '下拉列表';
					break;
				case 5:
					$res[$k]['type'] = '上传';
					break;
			}
		}
		return $res;
	}
	
	/**
	 * 获得字段信息列表
	 * @param number $page
	 * @param $limit
	 * @return 
	 */
	public function get_field_list($page, $limit = 10)
	{
		$listPage = array('data' => '', 'pageLink' => '');
		$where = ' 1=1';
		
		$sql = "SELECT a.id,a.name,a.type,a.data FROM (".$this->table('fields')." as a)  WHERE ".$where." ORDER BY a.id DESC";
		$result = $this->show_page($sql, $page, $limit);
		
		if($result)
		{
			foreach($result['result'] as $k => $v)
			{
				switch($v['type'])
				{
					case 0:
						$result['result'][$k]['type'] = '单行文本';
						break;
					case 1:
						$result['result'][$k]['type'] = '多行文本';
						break;
					case 2:
						$result['result'][$k]['type'] = '单选按钮';
						break;
					case 3:
						$result['result'][$k]['type'] = '多选按钮';
						break;
					case 4:
						$result['result'][$k]['type'] = '下拉列表';
						break;
					case 5:
						$result['result'][$k]['type'] = '上传';
						break;
				}
				$result['result'][$k]['name'] = hy_dig_filter($v['name'],1);
			}
		}
		return $result;
	}
	
	/**
	 * 判断是否有同名的字段
	 * @param $data
	 * @return
	 */
	public function check_same_data($data, $id = '')
	{
		if(empty($id))
		{
			$sql = "select count(*) from ".$this->table('fields')." where data='".$data."'";
		}
		else 
		{
			$sql = "select count(*) from ".$this->table('fields')." where data='".$data."' and id!=".$id;
		}
		return $this->getOne($sql);
	}
	
	/**
	 * 删除字段
	 * @param unknown $id
	 */
	public function del_field($id)
	{
		$sql = "";
	}
	
	/**
	 * 判断表单是否重复
	 * @param $name
	 * @param $id
	 * @return
	 */
	public function check_form_name($name, $id = '')
	{
		$where = '';
		if(!empty($id))
		{
			$where .= " and id!=".$id;
		}
		$sql = "select count(*) from ".$this->table('forms')." where name='".$name."'".$where;
		return $this->getOne($sql);
	}
	
	/**
	 * 获得管理字段信息
	 * @param $id
	 * @param $page
	 * @param $limit
	 * @return 
	 */
	public function get_form_info($id, $page, $limit = 10)
	{
		$sql = "select a.*, b.name, b.type as field_type, b.data FROM ".$this->table('forms_word')." as a left join ".$this->table('fields')." as b on a.fields_id=b.id where a.form_id=".$id;
		$result = $this->show_page($sql, $page, $limit);
		if($result['result'])
		{
			foreach($result['result'] as $k => $v)
			{
				switch($v['field_type'])
				{
					case 0:
						$result['result'][$k]['field_type'] = '单行文本';
						break;
					case 1:
						$result['result'][$k]['field_type'] = '多行文本';
						break;
					case 2:
						$result['result'][$k]['field_type'] = '单选按钮';
						break;
					case 3:
						$result['result'][$k]['field_type'] = '多选按钮';
						break;
					case 4:
						$result['result'][$k]['field_type'] = '下拉列表';
						break;
					case 5:
						$result['result'][$k]['field_type'] = '上传';
						break;
				}
				$result['result'][$k]['name'] = hy_dig_filter($v['name'],1);
			}
		}
		return $result;
	}
	
	/**
	 * 插入数据进表单
	 * @param $info
	 * @return 
	 */
	public function insert_table($info)
	{
		if($info)
		{
			$data = yf_set_insert($info);
			$sql = "insert into ".$this->table('forms_word')." (".$data['key'].") values (".$data['val'].")";
			$this->query($sql);
			$id = $this->insert_id();
			if($id)
			{
				$data_name = $this->getOne("select data from ".$this->table('fields')." where id=".$info['fields_id']);
				$sql = "ALTER TABLE ".$this->table('forms_log')."_".$info['form_id']." ADD `".$data_name."` ".$info['type']."(".$info['type_size'].")";
				$this->query($sql);
				return true;
			}
			else 
			{
				return false;
			}
		}
		else 
		{
			return false;
		}
	}
	
	/**
	 * 确认表单中是否已存在该字段
	 */
	public function check_fields_exist($form_id, $field_id)
	{
		$sql = "select count(*) from ".$this->table('forms_word')." where form_id=".$form_id." and fields_id=".$field_id;
		return $this->getOne($sql);
	}
	
	/**
	 * 更新表单字段显示顺序
	 */
	public function update_ordering($id, $val)
	{
		$sql = "update ".$this->table('forms_word')." set ordering=".$val." where id=".$id;
		$this->query($sql);
		return true;
	}
	
	/**
	 * 获取表单字段配置信息
	 * @param unknown $id
	 */
	public function get_one_field($id)
	{
		$sql = "select * from ".$this->table('forms_word')." where id=".$id;
		return $this->getRow($sql);
	}
	
	/**
	 * 更新表单字段信息
	 * @param $info
	 * @param $id
	 */
	public function update_word($info, $id)
	{
		if($id)
		{
			$error = 0;
			$old_data = $this->getRow("select * from ".$this->table('forms_word')." where id=".$id);
			if($old_data['type'] != $info['type'] || $old_data['type_size'] != $info['type_size'])
			{
				$error++;
			}
			$data = yf_set_update($info);
			$sql = "update ".$this->table('forms_word')." set ".$data." where id=".$id;
			$this->query($sql);
			
			if($error > 0)
			{
				$data_name = $this->getOne("select data from ".$this->table('fields')." where id=".$old_data['fields_id']);
				$sql = "ALTER TABLE ".$this->table('forms_log')."_".$old_data['form_id']." MODIFY `".$data_name."` ".$info['type']."(".$info['type_size'].")";
				$this->query($sql);
			}
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	/**
	 * 删除表字段
	 * @param $id
	 * @return 
	 */
	public function del_word($id)
	{
		$data = $this->getRow("select a.form_id, b.data from ".$this->table('forms_word')." as a left join ".$this->table('fields')." as b on a.fields_id=b.id where a.id=".$id);
		if(!empty($data))
		{
			$sql = "delete from ".$this->table('forms_word')." where id=".$id;
			$this->query($sql);
			$sql = "ALTER TABLE ".$this->table('forms_log')."_".$data['form_id']." drop column ".$data['data'];
			$this->query($sql);
			return true;
		}
		return false;
	}
	
	/**
	 * 删除表单
	 * @param unknown $id
	 * @return boolean
	 */
	public function del_form($id)
	{
		if($id)
		{
			$sql = "delete from ".$this->table('forms_word')." where form_id=".$id;
			$this->query($sql);
			$sql = "DROP TABLE IF EXISTS ".$this->table('forms_log')."_".$id.";";
			$this->query($sql);
			$sql = "delete from ".$this->table('forms')." where id=".$id;
			$this->query($sql);
			return true;
		}
		else 
		{
			return false;
		}
	}
	
	public function get_table_type($id)
	{
		$sql = "select b.data from ".$this->table('forms_word')." as a left join ".$this->table('fields')." as b on a.fields_id=b.id where a.form_id=".$id;
		$data = $this->getAll($sql);
		$key = array();
		if($data)
		{
			foreach($data as $k => $v)
			{
				$key[$k] = "`".$v['data']."`";
			}
		}
		return $key;
	}
	
}